package com.cskaoyan.shopping.controller.controllerbyq;

import com.cskaoyan.mall.commons.result.ResponseData;

import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.mall.constant.ShoppingRetCode;
import com.cskaoyan.mall.dto.ProductDetailRequest;
import com.cskaoyan.mall.dto.ProductDetailResponse;
import com.cskaoyan.shopping.dto.*;
import com.cskaoyan.shopping.form.PageInfo;
import com.cskaoyan.shopping.form.PageResponse;
import com.cskaoyan.shopping.service.IProductService;
import com.cskaoyan.shopping.service.servicebyq.ShoppingServiceByQ;
import com.netflix.client.http.HttpRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.net.URI;

/**
 * @author Qing
 * @since 2022/06/17 16:28
 */
@RestController
public class ShoppingController {

    @Autowired
    IProductService shoppingServiceByQ;




    @GetMapping("/shopping/product/*")
    public ResponseData getProductDetails(HttpServletRequest httpRequest){

        String requestURI = httpRequest.getRequestURI();
        String ids = requestURI.replace(httpRequest.getContextPath() + "/shopping/product/", "");

        Long id = null;
        try {
            id = Long.valueOf(ids);
        } catch (NumberFormatException e) {
            e.printStackTrace();
            return  new ResponseUtil().setErrorMsg("请求参数非法");
        }

        ProductDetailRequest request = new ProductDetailRequest();
         request.setId(id);

        ProductDetailResponse response = shoppingServiceByQ.getProductDetail(request);


        if (ShoppingRetCode.SUCCESS.getCode().equals(response.getCode())) {
            // 执行成功，直接返回前端需要的数据
            return new ResponseUtil().setData(response.getProductDetailDto());
        }
        // 调用失败
        return  new ResponseUtil().setErrorMsg(response.getMsg());

    }

    /**
     *
     * @author Q
     * @return
     */
    @GetMapping("/shopping/recommend")
    public ResponseData productRecommend(){

        RecommendResponse response = shoppingServiceByQ.getRecommendGoods();


        if (ShoppingRetCode.SUCCESS.getCode().equals(response.getCode())) {
            // 执行成功，直接返回前端需要的数据
            return new ResponseUtil().setData(response.getPanelContentItemDtos());
        }
        // 调用失败
        return  new ResponseUtil().setErrorMsg(response.getMsg());
    }


    /**
     *
     * @return
     */
    @PostMapping("/shopping/goods")
    public ResponseData  allGoods(@RequestBody PageInfo pageInfo){

        AllProductRequest allProductRequest = new AllProductRequest();
        allProductRequest.setPage(pageInfo.getPage());
        allProductRequest.setSize(pageInfo.getSize());
        allProductRequest.setSort(pageInfo.getSort());
        allProductRequest.setPriceGt(pageInfo.getPriceGt());
        allProductRequest.setPriceLte(pageInfo.getPriceLte());
        allProductRequest.setCid(pageInfo.getCid());
        AllProductResponse response = shoppingServiceByQ.getAllProduct(allProductRequest);

        PageResponse pageResponse = new PageResponse();
        pageResponse.setData(response.getProductDtoList());
        pageResponse.setTotal(response.getTotal());
        if (ShoppingRetCode.SUCCESS.getCode().equals(response.getCode())) {
            // 执行成功，直接返回前端需要的数据
            return new ResponseUtil().setData(pageResponse);
        }
        // 调用失败
        return  new ResponseUtil().setErrorMsg(response.getMsg());


    }

    @GetMapping("/shopping/goods")
    public ResponseData  allGoods(Integer page, Integer size,String sort, Integer priceGt,
             Integer priceLte, Long cid){

        AllProductRequest allProductRequest = new AllProductRequest();
        allProductRequest.setPage(page);
        allProductRequest.setSize(size);
        allProductRequest.setSort(sort);
        allProductRequest.setPriceGt(priceGt);
        allProductRequest.setPriceLte(priceLte);
        allProductRequest.setCid(cid);
        AllProductResponse response = shoppingServiceByQ.getAllProduct(allProductRequest);

        PageResponse pageResponse = new PageResponse();
        pageResponse.setData(response.getProductDtoList());
        pageResponse.setTotal(response.getTotal());
        if (ShoppingRetCode.SUCCESS.getCode().equals(response.getCode())) {
            // 执行成功，直接返回前端需要的数据
            return new ResponseUtil().setData(pageResponse);
        }
        // 调用失败
        return  new ResponseUtil().setErrorMsg(response.getMsg());


    }




}    